package com.springboot.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.news.domain.dto.OcrTextDTO;
import com.springboot.news.domain.po.OcrText;
import com.springboot.news.domain.query.OcrTextQuery;
import com.springboot.news.domain.vo.OcrTextVo;
import com.springboot.news.domain.vo.PageableVO;
import com.springboot.news.mapper.OcrTextMapper;
import com.springboot.news.service.OcrTextService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class OcrTextServiceImpl extends ServiceImpl<OcrTextMapper, OcrText> implements OcrTextService {


    @Resource
    private OcrTextMapper ocrTextMapper;

    @Override
    public PageableVO<OcrTextVo> tableList(OcrTextQuery dto) {
        QueryWrapper<OcrText> queryWrapper = new QueryWrapper<OcrText>();
        queryWrapper.like("content", dto.getContent());
        return PageableVO.fromPageRaw(page(dto.toPage(), queryWrapper), data -> {
            OcrTextVo vo = new OcrTextVo();
            BeanUtils.copyProperties(data, vo);
            return vo;
        });
    }

    @Override
    public void addOcrText(OcrTextDTO dto) {
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        OcrText ocrText = new OcrText();
        BeanUtils.copyProperties(dto, ocrText);
        save(ocrText);
    }

    @Override
    public void updateOcrText(OcrTextDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        dto.setCreateTime(LocalDateTime.now());
        OcrText ocrText = new OcrText();
        BeanUtils.copyProperties(dto, ocrText);
        updateById(ocrText);
    }

    @Override
    public void deleteOcrText(Integer id) {
        ocrTextMapper.deleteById(id);
    }

    @Override
    public OcrTextVo ocrTextDetail(Integer id) {
        OcrText ocrText = ocrTextMapper.selectById(id);
        OcrTextVo vo = new OcrTextVo();
        BeanUtils.copyProperties(ocrText, vo);
        return vo;
    }
}
